package com.sushang.controller;

import com.sushang.pojo.Category;
import com.sushang.pojo.Result;
import com.sushang.service.CategoryService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chengzhouli
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
/**
 * 新增文章分类
 *
 * **/
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success();
    }
    /***
     *
     *  文章分类列表
     *
     * **/
    @GetMapping
    public Result<List<Category>> list(){
        List<Category> cs=categoryService.list();
        return Result.success(cs);
    }
    /**
     * 获取文章分类详情
     *
     * **/
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        Category c = categoryService.findById(id);
        return Result.success(c);
    }
   /**
    * 更新文章分类
    * ***/
   @PutMapping
   public Result update(@RequestBody @Validated(Update.class) Category category){
       categoryService.update(category);
       return Result.success();
   }
   /**
    * 删除文章分类
    * **/
    @DeleteMapping
    public Result delete(Integer id){
        categoryService.deleteById(id);
        return Result.success();
    }
}
